package com.lanju.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.ContractDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.Contract;
import com.lanju.service.ContractService;
@Service
public class ContractServiceImpl implements ContractService {
	
	@Autowired
	ContractDao contractDao;
	@Autowired
	UserDao userDao;

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public  Map<String, Object> createContract(Contract contract) {
		Map<String,Object> result=new HashMap<String, Object>();
		try {
			contractDao.createContract(contract);
			result.put("success",true);
			result.put("msg","创建成功");
			result.put("contractId", contract.getContractId());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success",false);
			result.put("msg","创建失败");
			result.put("contractId", null);
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> delContract(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<>();
		try {
			Contract contract =contractDao.getContract(map);
			if(contract!=null){
				contractDao.delContract(contract);
				result.put("success", true);
				result.put("msg", "删除成功");
			}else{
				result.put("success", false);
				result.put("msg", "合同不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "操作出错");
		}
		return result;
	}

	@Override
	public Map<String, Object> getConstractDetailById(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<>();
		try {
			Contract contract=contractDao.getContract(map);
			result.put("contractDetail", contract);
			if(!contract.getExpectedFixedIncomeSet().isEmpty())
			result.put("contractIncome", contractDao.getExpectedFixedIncomeList(map));
			result.put("vacantperiodSet", contractDao.getVacantPeriodList(map));
			result.put("success", true);
			result.put("flag", map.get("flag").toString());
			result.put("login", map.get("login"));
			result.put("msg", "返回成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("rows", null);
			result.put("success", false);
			result.put("msg", "返回失败");
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateConstract(Contract contract) {
		Map<String,Object> result=new HashMap<>();
		try {
			contractDao.updateConstract(contract);
			result.put("success", true);
			result.put("msg", "修改成功");
			result.put("contractId", contract.getContractId());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "修改失败");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getContractList(Map<String, Object> map) {
		Map result=new HashMap<>();
		try {
			result.put("rows", contractDao.getContractList(map));
			result.put("flag", map.get("flag").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> signComtract(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<String,Object>();
		try {
			Contract contract=contractDao.getContract(map);
			if(contract !=null){
				contract.setStatus(1);
				contractDao.signComtract(contract);
				result.put("success", true);
				result.put("msg", "签名成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "签名失败");
		}
		return result;
	}

	@Override
	public Map<String, Object> getUserInfo(Map<String, Object> map) {
		try {
//			map.get("city").toString()
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
